//
//  NYSegment.m
//  NYSegmentedControl
//
//  Copyright (c) 2014 Nealon Young. All rights reserved.
//
//  https://github.com/nealyoung/NYSegmentedControl
//

#import "NYSegment.h"



//static CGFloat const kMinimumSegmentWidth = 68.0f;

@implementation NYSegment

- (instancetype)initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(nullable UIImage *)selectedImage{
    self = [self initWithFrame:CGRectZero];
    if (self) {
        self.titleLabel.text = title;
        self.image = image;
        self.selectedImage = selectedImage;
        self.imgView.image = image;
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.userInteractionEnabled = NO;
        self.titleLabel = [[UILabel alloc] initWithFrame:self.frame];
        self.imgView = [[UIImageView alloc] initWithFrame:frame];
        self.imgView.backgroundColor = [UIColor clearColor];
        self.imgView.contentMode = UIViewContentModeCenter;
//        self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        
        self.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        self.titleLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:self.titleLabel];
        [self addSubview:self.imgView];
//        [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(self);
//            make.centerX.equalTo(self).offset(-40);
//            make.height.equalTo(@25);
//            make.width.equalTo(@25);
//        }];
//        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(self);
//            make.centerX.equalTo(self).offset(-15);
//            make.height.equalTo(@20);
//            make.width.equalTo(@60);
//        }];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.imgView.frame = CGRectMake(self.frame.size.width/2.0 - 40, 0, 25, 25);
    self.imgView.center = CGPointMake(self.imgView.center.x, self.frame.size.height/2.0);
    self.titleLabel.frame = CGRectMake(self.frame.size.width/2.0 - 15, 0, 60, 20);
    self.titleLabel.center = CGPointMake(self.titleLabel.center.x, self.frame.size.height/2.0);
    
}

//- (CGSize)sizeThatFits:(CGSize)size {
//    CGSize sizeThatFits = [self.titleLabel sizeThatFits:size];
//    return CGSizeMake(MAX(sizeThatFits.width * 1.4f, kMinimumSegmentWidth), sizeThatFits.height);
//}

@end
